package controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;

/**
 * 系统退出
 */
@Controller
@RequestMapping(value = "/logout")
public class LogoutController {

    private static Logger logger = LoggerFactory.getLogger(LogoutController.class);

    @RequestMapping(value = "/tologin")
    public String toLogin(HttpServletRequest request, HttpServletResponse response){
        long logoutTime = System.currentTimeMillis();
        logger.info("系统退出时间===> " + new SimpleDateFormat("YYYY年mm月dd日 HH时:mm分:ss秒").format(Long.valueOf(logoutTime)));
        //设置提示语句
        request.getSession().setAttribute("msg", "退出成功!即将跳转首页");
        //销毁session对象
        request.getSession().invalidate();
        //shiro权限验证
        Subject subject = SecurityUtils.getSubject();
        if(subject.isAuthenticated()) {
            subject.logout();
        }
        //重定向到首页
        return  "redirect:/login/tologin";
    }
}
